package com.example.javamaday.leetcode;

import com.example.javamaday.leetcode.base.TreeNode;

public class LeetCodeTest0543 {

    public static void main(String[] args) {

    }

    private int maxDia = 0;

    public int diameterOfBinaryTree(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int max = maxDepth(root.left) + maxDepth(root.right);
        maxDia = Math.max(max, maxDia);
        diameterOfBinaryTree(root.left);
        diameterOfBinaryTree(root.right);
        return maxDia;
    }


    public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.right == null && root.left == null) {
            return 1;
        }
        return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
    }

}
